<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/16
 * Time: 9:33
 */
namespace Service\Controller;
use Think\Controller;

class OaUserController extends Controller
{
    protected $modelName = CONTROLLER_NAME;

    //通过 USER_ID 查找
    public function getUser(){
        $req = I('param.');
        if($req['USER_ID'] == 'all'){
            $data = D($this->modelName)->relation(true)->select();
            $count = D($this->modelName)->count();
            $res['stat'] = $data ? 1 : 0;
            $res['msg'] = $data ? '找到'.$count.'条数据' : '没有找到数据';
            $res['data'] = $data;
        }elseif($req['USER_ID']){
            $where['USER_ID'] = $req['USER_ID'];
            $data = D($this->modelName)->where($where)->relation(true)->find();
            $res['stat'] = $data ? 1 : 0;
            $res['msg'] = $data ? '找到1条数据' : '没有找到数据';
            $res['data'] = $data;
        }else{
            $res['stat'] = 0;
            $res['msg'] = '参数提交不正确';
        }
        $this->ajaxReturn($res);
    }

    public function cp(){
        if(!S('oaUser')){
            $data = D('OaUser')->relation(array('priv','dept'))->select();
            foreach($data as $k=>$v){
                $oaUser[$k]['UID'] = $v['UID'];
                $oaUser[$k]['USER_ID'] = $v['USER_ID'];
                $oaUser[$k]['USER_NAME'] = $v['USER_NAME'];
                $oaUser[$k]['USER_PRIV'] = $v['USER_PRIV'];
                $oaUser[$k]['PRIV_NAME'] = $v['priv']['PRIV_NAME'];
                $oaUser[$k]['USER_PRIV_OTHER'] = $v['USER_PRIV_OTHER'];
                $oaUser[$k]['DEPT_ID'] = $v['DEPT_ID'];
                $oaUser[$k]['DEPT_NAME'] = $v['dept']['DEPT_NAME'];
                $oaUser[$k]['DEPT_ID_OTHER'] = $v['DEPT_ID_OTHER'];
                $oaUser[$k]['real_name'] = $v['USER_NAME'];
//                $oaUser[$k]['sex'] = $v['SEX'];
                $oaUser[$k]['birthday'] = $v['BIRTHDAY'];
                $oaUser[$k]['mobile'] = $v['MOBIL_NO'];
                $oaUser[$k]['email'] = $v['EMAIL'];
                $oaUser[$k]['qq'] = $v['OICQ_NO'];
            }
            S('oaUser',$oaUser,7200);
        }
//        echo '<pre>';
//        var_dump(S('oaUser'));die;
        foreach(S('oaUser') as $k=>$v){
            $userHas = M('User')->where(array('real_name'=>$v['real_name']))->find();
            if($userHas) {
                $dd['crm_id'] = $userHas['id'];
                $ww['USER_NAME'] = $userHas['real_name'];
                M('OaUser')->data($dd)->where($ww)->save();
                $this->tobeAssistant($userHas);
                var_dump($v['real_name']);
                continue;
            }
            $user = $v;
            $user['USER_NAME'] = $v['real_name'];
//            unset($user['real_name']);
//            unset($user['sex']);
//            unset($user['birthday']);
//            unset($user['mobile']);
//            unset($user['email']);
//            unset($user['qq']);
            $user['create_time'] = time();
            $user['password'] = md5(123456);
            $user['sex'] = $v['sex'] ? 'F' : 'M';
            $user['is_active'] = 1;
            if($v['USER_ID'] == 'admin'){
                $user['id'] = 1;
                $user['user_name'] = 'Administrator';
                $user['is_super'] = 1;
            }else{
                $user['user_name'] = $v['real_name'];
                $user['is_super'] = 0;
            }
            dump($user);
            M('User')->data($user)->add();
//            echo D('oaUser')->getLastSql();
//            echo D('User')->getDbError();exit;
        }
    }

    public function goAssistant(){
        $users = M('User')->select();
        foreach ($users as $k=>$v){
            $this->tobeAssistant($v);
        }
    }

    public function tobeAssistant($data)
    {
        $is1 = strpos($data['USER_PRIV_OTHER'],'65');
        $is2 = strpos($data['USER_PRIV_OTHER'],'62');
        $is3 = strpos($data['USER_PRIV'],'65');
        $is4 = strpos($data['USER_PRIV'],'62');
        echo '<pre>';
        var_dump($data);
        var_dump($is1.$is2.$is3.$is4);
        echo '</pre>';
        if($is1!==false || $is2!==false || $is3!==false || $is4!==false){
            M('RoleUser')->where(array('user_id'=>$data['id']))->delete();
            $_data['user_id'] = $data['id'];
            $_data['role_id'] = 4;
            echo $data['id'];
            M('RoleUser')->data($_data)->add();
        }
    }
}